/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package amd.core;

import amd.core.textcommand.Command;
import amd.core.textcommand.CommandWord;
import amd.core.textcommand.Parser;
import java.util.ArrayList;

/**
 *
 * @author Latifah Dwi Nurachma
 */
public class AmdLsa {
    private Parser parser;
    private TextCommandListener listener;
    private int page;
    private int maxPage;
    private String inputKeyword;
    private ArrayList<ArrayList<String>> listDoc;
    private String idDocReff;
    private String keteranganHasilPencarian;

    public AmdLsa() throws Exception{
        parser = new Parser();
        listener = new TextCommandListener();
        page=0;
        maxPage=0;
        listDoc = new ArrayList<ArrayList<String>>();
        idDocReff = "";
        keteranganHasilPencarian="";
    }

    public void run(){
        boolean finished=false;

        printWelcome();
        while(!finished){
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
    }

    private boolean isNumber(String secondWord) {
        return secondWord.equalsIgnoreCase("1") ||
               secondWord.equalsIgnoreCase("2") ||
               secondWord.equalsIgnoreCase("3") ||
               secondWord.equalsIgnoreCase("4") ||
               secondWord.equalsIgnoreCase("5") ||
               secondWord.equalsIgnoreCase("6") ||
               secondWord.equalsIgnoreCase("7") ||
               secondWord.equalsIgnoreCase("8") ||
               secondWord.equalsIgnoreCase("9") ||
               secondWord.equalsIgnoreCase("10");
    }

    private void printWelcome() {
        System.out.println("-------------------------------------");
        System.out.println("----- APLIKASI MANAJEMEN DOKUMEN    ");
        System.out.println("               DENGAN LSA       -----");
        System.out.println("-------------------------------------");
        System.out.print("\n");
        printHelp();

    }

    private boolean processCommand(Command command) {
        boolean wantToQuit = false;

        CommandWord commandWord = command.getCommandWord();

        if(commandWord == CommandWord.UNKNOWN) {
            System.out.println("Salah command...");
            return false;
        }

        else if (commandWord == CommandWord.HELP) {
            printHelp();
        }

        else if(commandWord == CommandWord.EXTRACT){
            listener.ekstrakDokumen();
        }

        else if (commandWord == CommandWord.SEARCH) {
            page=0;
            if(command.hasSecondWord()){
                inputKeyword = command.getSecondWord();
                //listDoc = listener.searchDocumentByKeyword(inputKeyword, page);
                //keteranganHasilPencarian = "";
                search();
                showResult();
            }
            else{
                System.out.println("Keyword kosong. Tidak melakukan pencarian dokumen.");
            }
        }

        else if (commandWord == CommandWord.DETAIL){
            if(command.hasSecondWord()){
                if(isNumber(command.getSecondWord())){
                    int i = Integer.valueOf(command.getSecondWord());
                    if(!listDoc.isEmpty() && listDoc.get(0).size()>=i){
                        showDetail(listDoc.get(0).get((i-1)));
                    }
                    else{
                        System.out.println("Tidak ada dokumen hasil pencarian.");
                    }
                }
                else{
                    System.out.println("Nomor dokumen tidak valid.");
                }
            }
            else{
                System.out.println("Masukkan nomor dokumen yang akan dilihat informasinya.");
            }
        }

        else if (commandWord == CommandWord.READ){
            if(!idDocReff.equalsIgnoreCase("")){
                String path = listener.getTargetPath();
                listener.openPdfDocument(path + "/" + idDocReff + ".pdf");
            }
            else{
                System.out.println("Belum ada dokumen yang akan dibaca.");
            }
        }

        else if(commandWord == CommandWord.RESULT){
            showResult();
        }

        else if (commandWord == CommandWord.LOG){
            listener.viewLog();
            System.out.println("Menapilkan file log");
        }

        else if (commandWord == CommandWord.QUIT) {
            wantToQuit = quit(command);
        }

        else if (commandWord == CommandWord.UPLOAD){
            if(command.hasSecondWord()){
                listener.uploadDocument(command.getSecondWord());
            }
            else{
                System.out.println("Harap masukan path dokumen yang akan diupload.");
            }
        }

        else if(commandWord == CommandWord.NEXT){
            if(page<(maxPage-1)){
                page = page++;
                search();
            }
        }

        else if (commandWord == CommandWord.PREV){
            if(page>0){
                page--;
                search();
            }
        }
        // else command not recognised.
        return wantToQuit;
    }

    private boolean quit(Command command)
    {
        if(command.hasSecondWord()) {
            System.out.println("Quit what?");
            return false;
        }
        else {
            listener.close();
            return true;  // signal that we want to quit
        }
    }

    private void printHelp() {
        //System.out.println();
        System.out.println("Perintah: search, upload, detail, read, result, extract, log");
    }

    private void search() {
        listDoc = listener.searchDocumentByKeyword(inputKeyword, page);
        keteranganHasilPencarian = "Hasil dencarian dokumen berdasarkan keyword \'" + inputKeyword +"\'";
    }

    private void showDetail(String secondWord) {
        ArrayList<String> detailDoc = listener.getDocumentInfo(secondWord);
        page=0;
        idDocReff = detailDoc.get(0);

        //listDoc = listener.searchDocumentSimilar(idDocReff, page);
        System.out.println("Judul Dokumen : " + detailDoc.get(1));
        System.out.println("Abstrak       : ");
        System.out.println(detailDoc.get(2));
        listDoc = listener.searchDocumentSimilar(idDocReff, page);
        if(!listDoc.isEmpty() && listDoc.get(0).size()>0){
            System.out.println("-------------------------------------");
            keteranganHasilPencarian= "Dokumen lain yang similar dengan \'" + detailDoc.get(1)+ "\'";
            showResult();
        }
        else{
            System.out.println("Tidak terdapat dokumen yang similar.");
        }

   }


    private void showResult(){
        if(!listDoc.isEmpty() && listDoc.get(0).size()>0){
            int i=0;
            int nDocResult = listDoc.get(0).size();
            System.out.println("-------------------------------------");
            System.out.println(keteranganHasilPencarian);
            while(i<nDocResult){
                System.out.println("-- " + (i+1)+" " + listDoc.get(1).get(i));
                i++;
            }
        }
        else{
            System.out.println("Tidak ada dokumen hasil pencarian.");
        }
    }

}
